<?php


class tx_zlib_viewhelper_rte extends tx_zlib_abstract_viewhelper
{

	private static $_rteInstanceCounter;

	/**
	 * Transforms content saved by the RTE. This is the shortform
	 * of rte()->transform().
	 *
	 * If no arguments are passed, returns a tx_zlib_viewhelper_rte object.
	 *
	 * @param string $input
	 * @return string|tx_zlib_viewhelper_rte
	 */
	public function rte($input = null)
	{
        if (0 == func_num_args()) {
            return $this;
        }
		return $this->transform($input);
	}

	/**
	 * Transforms content saved by the RTE into suitable html formats.
	 * Eg. transforms TYPO3-typolinks (<link></link>) into HTML <a> anchors
	 *
	 * @param string $input
	 * @return string
	*/

	public function transform($input) {
		return $this->getPi()->pi_RTEcssText($input);
	}

	/**
	 * Enter description here...
	 *
	 * @param string $field
	 * @param string $value
	 * @return unknown
	 */
	public function editor($field, $value)
	{

		require_once(t3lib_extMgm::extPath('rtehtmlarea').'pi2/class.tx_rtehtmlarea_pi2.php');

		self::$_rteInstanceCounter++;

		$this->getPi()->RTEObj;
		$this->getPi()->docLarge = 0;
		$this->getPi()->RTEcounter = self::$_rteInstanceCounter;
		$this->getPi()->formName = '';
		$this->getPi()->additionalJS_initial = '';
		$this->getPi()->additionalJS_pre = array();
		$this->getPi()->additionalJS_post = array();
		$this->getPi()->additionalJS_submit = array();
		$this->getPi()->PA = array(
			'itemFormElName' =>  '',
			'itemFormElValue' => '',
		);
		$this->getPi()->specConf = array(
			'rte_transform' => array(
				'parameters' => array('mode' => 'ts_css')
			)
		);
		$this->getPi()->thisConfig = array();
		$this->getPi()->RTEtypeVal = 'text';
		$this->getPi()->thePidValue = $GLOBALS['TSFE']->id;

		if(!$this->getPi()->RTEObj) $this->getPi()->RTEObj = t3lib_div::makeInstance('tx_rtehtmlarea_pi2');
		if($this->getPi()->RTEObj->isAvailable()) {
			$this->getPi()->PA['itemFormElName'] = $this->getPi()->prefixId . '[' . $field . ']';
			$this->getPi()->PA['itemFormElValue'] = $value;
			$this->getPi()->thePidValue = $GLOBALS['TSFE']->id;
			$RTEItem = $this->getPi()->RTEObj->drawRTE($this->getPi(), '', '', $row=array(), $this->getPi()->PA, $this->getPi()->specConf, $this->getPi()->thisConfig, $this->getPi()->RTEtypeVal, '', $this->getPi()->thePidValue);

			$markerArray['ADDITIONALJS_PRE'] = $this->getPi()->additionalJS_initial.'
			<script type="text/javascript">'. implode(chr(10), $this->getPi()->additionalJS_pre).'
			</script>';
			$markerArray['ADDITIONALJS_POST'] = '
			<script type="text/javascript">'. implode(chr(10), $this->getPi()->additionalJS_post).'
			</script>';
			$markerArray['ADDITIONALJS_SUBMIT'] = implode(';', $this->getPi()->additionalJS_submit);
			$markerArray['FORM_RTE_ENTRY'] = $RTEItem;
		}

		$onsubmit = "
			<script type=\"text/javascript\">
				/*<![CDATA[*/

				function rteHandler" . $this->getPi()->RTEcounter . "()
				{
					" . $markerArray['ADDITIONALJS_SUBMIT'] . "
				}

				/*]]>*/
			</script>";

		return $onsubmit
		 . $markerArray['ADDITIONALJS_PRE']
		 . $markerArray['FORM_RTE_ENTRY']
		 . $markerArray['ADDITIONALJS_POST'];

	}

	private function _testPiMemberVars()
	{
		/*
		var $RTEObj;
	    var $strEntryField;
	    var $docLarge = 0;
	    var $RTEcounter = 0;
	    var $formName;
	    var $additionalJS_initial = '';		// Initial JavaScript to be printed before the form (should be in head, but cannot due to IE6 timing bug)
		var $additionalJS_pre = array();	// Additional JavaScript to be printed before the form
		var $additionalJS_post = array();	// Additional JavaScript to be printed after the form
		var $additionalJS_submit = array();	// Additional JavaScript to be executed on submit
	    var $PA = array(
	            'itemFormElName' =  '',
	            'itemFormElValue' = '',
	            );
	    var $specConf = array();
	    var $thisConfig = array();
	    var $RTEtypeVal = 'text';
	    */
	}

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_rte.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/views/helpers/class.tx_zlib_viewhelper_rte.php']);
}

